home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: connectInitialize.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:54 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #ifdef JUNK
- #ifdef defined(c_plusplus) & !defined(__cplusplus)
- # define getservbyname _old_getservbyname
- # include <netdb.h>
- # undef getservbyname
- extern servent *getservbyname(char *, char *);
- #else
- # include <netdb.h>
- #endif c_plusplus
- #endif JUNK
-
- #include "sysdefs.h"
- #include <fcntl.h>
- #include "ess.h"
- #include "checking.h"
- #include "trace.h"
- #include "error.h"
- #include "list.h"
- #include "tid.h"
- #include "io.h"
- #include "lock.h"
- #include "object.h"
- #include "msgdefs.h"
- #include "thread.h"
- #include "semaphore.h"
- #include "link.h"
- #include "host.h"
- #include "bitvec.h"
- #include "msgvector.h"
- #include "msg_funcs.h"
- #include "util_funcs.h"
- #include "msg_globals.h"
-
-
- #if defined(__cplusplus) && !defined(hpux)
- #elif c_plusplus
- #elif hpux
- #else
- extern SERVENT *getservbyname(char *, char *);
- #endif
-
- void
- connectInitialize ()
-
- {
- SOCKADDR ConnectAddr;
- register LINK *linkTcp, *linkUdp;
- register SERVENT *_servent=(SERVENT *)0;
- int length;
- int retryLimit = 20;
- int retryCount;
- int socketBufSize = 16*1024; /* 16K socket buffers */
-
- TRACE(TR_MSG, TR_LEVEL_1);
-
- /*
- * get the port on which to listen for client connect requests.
- */
- if ( (!ServePort) &&
- ((_servent = getservbyname(ServeName, "tcp")) == NULL)) {
-
- SM_ERROR(TYPE_FATAL, errno);
- }
-
- /*
- * initialize the address
- */
- ConnectAddr.sin_family = AF_INET;
- ConnectAddr.sin_addr.s_addr = INADDR_ANY;
- if(_servent) {
- ConnectAddr.sin_port = _servent->s_port;
- } else {
- ConnectAddr.sin_port = ServePort;
- }
- TRPRINT(TR_MSG, TR_LEVEL_2, ("host inet addr:%s",
- inet_ntoa(*(struct in_addr *)&ConnectAddr.sin_addr.s_addr)));
-
- /*
- * create the socket
- */
- if ((fdTcp = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-
- SM_ERROR(TYPE_FATAL, errno);
- }
- TRPRINT(TR_MSG, TR_LEVEL_2, ("connection socket:%d", fdTcp));
-
- if ((fdUdp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-
- SM_ERROR(TYPE_FATAL, errno);
- }
- TRPRINT(TR_MSG, TR_LEVEL_2, ("datagram socket:%d", fdUdp));
-
- /*
- * set socket to not delay sending data and enlarge the socket
- * buffer size
- */
- if (setSocketOptions(fdTcp, socketBufSize, socketBufSize) != esmNOERROR) {
- SM_ERROR(TYPE_FATAL, esmINTERNAL);
- }
-
- /*
- * set the socket to close on exec for disk processes
- */
- if (fcntl(fdTcp, F_SETFD, NULL) < 0) {
- SM_ERROR(TYPE_FATAL, errno);
- }
- if (fcntl(fdUdp, F_SETFD, NULL) < 0) {
- SM_ERROR(TYPE_FATAL, errno);
- }
-
- /*
- * mark the link as used
- */
- linkTcp = &(Links[fdTcp]);
- listRemove( &(linkTcp->list) );
- linkTcp->linkClass = CL_CONNECT;
-
- linkUdp = &(Links[fdUdp]);
- listRemove( &(linkUdp->list) );
- linkUdp->linkClass = CL_DGRAM;
-
- /*
- * get the size of the structure
- */
- length = sizeof(ConnectAddr);
-
- /*
- * bind the address to the socket. This may fail because
- * brain-damaged OS's don't like to free sockets right away.
- * So, try a few times.
- */
- for (retryCount = 0; retryCount < retryLimit; retryCount++) {
-
- if (bind(fdTcp, (struct sockaddr *) &ConnectAddr, sizeof(ConnectAddr)) < 0)
- {
-
- if (errno == EADDRINUSE && (retryCount+1 < retryLimit)) {
- SM_ERROR(TYPE_LOG, errno);
- fprintf(stderr, "Retrying in 10 seconds ...\n");
- sleep(10 /*seconds*/);
- } else {
- fprintf(stderr, "Port %d/tcp is in use by another process.\n",
- ntohs(ConnectAddr.sin_port));
-
- SM_ERROR(TYPE_STOP, errno);
- }
- } else {
- /*
- * The Udp socket really SHOULD be free if the tcp one is.
- * We assume that noone is going to grab this
- * unofficially "well-known" port.
- */
- if (bind(fdUdp, (struct sockaddr *) &ConnectAddr,
- sizeof(ConnectAddr)) < 0) {
-
- fprintf(stderr, "Port %d/udp is in use by another process.\n",
- ntohs(ConnectAddr.sin_port));
-
- SM_ERROR(TYPE_STOP, errno);
- }
- /* success to exit retry loop */
- break;
- }
-
- }
- /*
- * get the name of the socket
- */
- if (getsockname(fdTcp, (struct sockaddr *) &ConnectAddr, &length)) {
-
- SM_ERROR(TYPE_FATAL, errno);
- }
- TRPRINT(TR_MSG, TR_LEVEL_2, ("socket has port:%d", ntohs(ConnectAddr.sin_port)));
-
- /*
- * set the receive information
- */
- setLink(linkTcp);
- setLink(linkUdp);
-
- /*
- * set to accept connections
- */
- if (listen(fdTcp, 5) < 0) {
-
- SM_ERROR(TYPE_STOP, errno);
- }
- TRPRINT(TR_MSG, TR_LEVEL_2, ("listening on link:%d", fdTcp));
-
- /*
- * set the status of the link to listen
- */
- linkTcp->flags = LINK_LISTEN;
- linkUdp->flags = LINK_LISTEN; /* poor word for datagram use */
- }
-